package com.shenghua.service.impl;

import com.alibaba.fastjson.JSON;
import com.shenghua.dao.mapper.LimitsMapper;
import com.shenghua.dao.mapperX.LimitsMapperX;
import com.shenghua.dao.model.Limits;
import com.shenghua.dao.model.LimitsExample;
import com.shenghua.service.LimitsService;
import com.shenghua.utils.ListUtils;
import com.shenghua.utils.StringUtils;
import com.shenghua.utils.beans.Dto;
import com.shenghua.utils.beans.ErrorCode;
import com.shenghua.utils.sysutil.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.BeanInfo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/3/20.
 */
@Service
public class LimitsServiceImpl implements LimitsService {
    Logger logger = LoggerFactory.getLogger(LimitsServiceImpl.class);
    @Autowired
    private LimitsMapper limitsMapper;
    @Autowired
    private LimitsMapperX limitsMapperX;

    @Override
    public Dto<Limits> addNewLimits(Limits limits) {
        Dto<Limits> dto = new Dto<>();
        if(null == limits){
            logger.debug("[LimitsServiceImpl] addNewLimits 权限信息为空！");
            dto.ReturnFail(new Limits(), ErrorCode.PARAMETER_IS_NULL);
            return dto;
        }
        if(null == limits.getId()){
            logger.debug("[LimitsServiceImpl] addNewLimits 上级菜单不能为空！");
            dto.ReturnFail(new Limits(), ErrorCode.PARAMETER_IS_NULL);
            return dto;
        }
        // 获取同一parentCode的最大limitCode
        int parentCode = limits.getId();
        limits.setId(null);
        Integer maxLimitCode = limitsMapperX.selectMaxLimitCode();
        if(maxLimitCode == null) {
            logger.debug("[LimitsServiceImpl] addNewLimits 获取权限编码失败！");
            dto.ReturnFail(new Limits(), ErrorCode.MOULD_ADD_FAIL);
            return dto;
        }
        limits.setLimitCode(maxLimitCode + 1);
        // 赋值
        if(StringUtils.isBlank(limits.getLimitSort())) {
            // 同一parentCode，同一limitType的最大limitSort
            String limitType = limits.getLimitType();
            Integer maxLimitSort = limitsMapperX.selectMaxLimitSortSameParentCodeSameLimitType(parentCode, limitType);
            if(maxLimitSort==null) {
                maxLimitSort = 1;
            }
            limits.setLimitSort(maxLimitSort+1);
        }

        limits.setCreated(new Date());

        LimitsExample limitsExample = new LimitsExample();
        LimitsExample.Criteria criteria = limitsExample.createCriteria();
        criteria.andLimitCodeEqualTo(limits.getLimitCode());
        List<Limits> limitsList = limitsMapper.selectByExample(limitsExample);
        if(ListUtils.isNotEmpty(limitsList)) {
            logger.error("[LimitsServiceImpl] addNewLimits 权限编号已经存在！ limits={}", JSON.toJSONString(limits));
            dto.ReturnFail(new Limits(), ErrorCode.LIMITS_ADD_FAIL);
            return dto;
        }
        int result = limitsMapper.insert(limits);
        if(result<=0){
            logger.error("[LimitsServiceImpl] addNewLimits 权限信息添加失败！");
            dto.ReturnFail(new Limits(), ErrorCode.LIMITS_ADD_FAIL);
            return dto;
        }
        dto.ReturnSuccess(limits,ErrorCode.LIMITS_ADD_SUCCESS);
        logger.info("[LimitsServiceImpl] addNewLimits 权限信息添加成功！");
        return dto;
    }

    @Override
    public Dto<Limits> editCurrentLimits(Limits limits) {
        Dto<Limits> dto = new Dto<>();
        if(null == limits){
            logger.debug("[LimitsServiceImpl] editCurrentLimits 权限信息为空！");
            dto.ReturnFail(new Limits(), ErrorCode.PARAMETER_IS_NULL);
            return dto;
        }
        if(null == limits.getId()){
            logger.debug("[LimitsServiceImpl] editCurrentLimits 上级菜单不能为空！");
            dto.ReturnFail(new Limits(), ErrorCode.PARAMETER_IS_NULL);
            return dto;
        }
        Integer parentCode = limits.getParentCode();
        // 赋值
        if(StringUtils.isBlank(limits.getLimitSort())) {
            // 同一parentCode，同一limitType的最大limitSort
            String limitType = limits.getLimitType();
            Integer maxLimitSort = limitsMapperX.selectMaxLimitSortSameParentCodeSameLimitType(parentCode, limitType);
            if(maxLimitSort == null) {
                maxLimitSort = 1;
            }
            limits.setLimitSort(maxLimitSort+1);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
        String updated = sdf.format(limits.getUpdated());
        try {
            limits.setUpdated(sdf.parse(updated));
        } catch (ParseException e) {
            e.printStackTrace();
            limits.setUpdated(new Date());
            logger.info("时间解析异常！");
        }
        int result = limitsMapper.updateByPrimaryKeySelective(limits);
        if(result<=0){
            logger.error("[LimitsServiceImpl] editCurrentLimits 权限信息编辑失败！");
            dto.ReturnFail(new Limits(), ErrorCode.LIMITS_EDIT_FAIL);
            return dto;
        }
        dto.ReturnSuccess(limits,ErrorCode.LIMITS_EDIT_SUCCESS);
        logger.info("[LimitsServiceImpl] editCurrentLimits 权限信息编辑成功！");
        return dto;
    }

}
